package domain.entities;

import domain.annotations.TableName;
import util.MD5Helper;
import util.UUIDHelper;

import java.sql.Timestamp;

/**
 * 用户实体
 *
 * @author wyl
 */
@TableName("T_User")
public class User implements Cloneable {

    private String id;
    private String username;
    private String nickname;
    private String password;
    private Long createTime;
    private Long jwtVersion;

    private User() {

    }

    public User(String username, String nickname, String password) throws Exception {
        this.id = UUIDHelper.create();
        this.createTime = System.currentTimeMillis();
        this.username = username;
        this.nickname = nickname;
        this.password = MD5Helper.encrypt(password);
        this.jwtVersion = 0L;
    }

    public String getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) throws Exception {
        this.password = MD5Helper.encrypt(password);
    }

    public Timestamp getCreateTime() {
        return new Timestamp(createTime);
    }

    public Long getJwtVersion() {
        return jwtVersion;
    }


    public void jwtVersionIncrease() {
        this.jwtVersion++;
    }


    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", nickname='" + nickname + '\'' +
                ", password='" + password + '\'' +
                ", createTime='" + createTime + '\'' +
                '}';
    }

    @Override
    public User clone() throws CloneNotSupportedException {
        return (User) super.clone();
    }
}

